﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace MikroMagazaObject
{
    public static class Functions
    {
        public static string Mid(this string value, int start, int lenght)
        {
            if (value.Length > start + lenght)
            {
                return value.Substring(start, lenght);
            }
            else
            {
                return value.Substring(start);
            }
        }

        public static string Space(this string value, int lenght)
        {
            return value + new string(' ', lenght);
        }

        public static DateTime ToDateTime(this string value)
        {
            return new DateTime(int.Parse(value.Split('/')[2]), int.Parse(value.Split('/')[1]), int.Parse(value.Split('/')[0]));
        }

        public static string AyAdi(this int value)
        {
            switch (value)
            {
                case 1:
                    return "1.Ocak";
                case 2:
                    return "2.Şubat";
                case 3:
                    return "3.Mart";
                case 4:
                    return "4.Nisan";
                case 5:
                    return "5.Mayıs";
                case 6:
                    return "6.Haziran";
                case 7:
                    return "7.Temmuz";
                case 8:
                    return "8.Ağustos";
                case 9:
                    return "9.Eylül";
                case 10:
                    return "10.Ekim";
                case 11:
                    return "11.Kasım";
                case 12:
                    return "12.Aralık";
                default:
                    return "";
            }
        }

        public static DateTime RemoveTime(this DateTime value)
        {
            return new DateTime(value.Year, value.Month, value.Day);
        }

        public static string ToFullSqlString(this DateTime tarih)
        {
            return string.Format("{0}-{1}-{2} {3}:{4}:{5}", tarih.Year, tarih.Month, tarih.Day, tarih.Hour, tarih.Minute, tarih.Second);
        }

        public static string ToSqlString(this DateTime tarih)
        {
            return string.Format("{0}-{1}-{2} 00:00:00", tarih.Year, tarih.Month, tarih.Day);
        }

        public static DateTime ToFullDateTime(this string tarih)
        {
            string[] date = tarih.Split(' ')[0].Split('-');
            string[] saat = tarih.Split(' ')[1].Split(':');
            return new DateTime(int.Parse(date[0]), int.Parse(date[1]), int.Parse(date[2]), int.Parse(saat[0]), int.Parse(saat[1]), int.Parse(saat[2]));
        }

        public static Byte[] Serialize(SettingsObject db)
        {
            MemoryStream mem = new MemoryStream();
            BinaryFormatter bformatter = new BinaryFormatter();
            bformatter.Serialize(mem, db);
            mem.Close();
            return mem.ToArray();
        }

        public static SettingsObject DeSerialize(Byte[] data)
        {
            SettingsObject db = null;
            MemoryStream mem = new MemoryStream(data);
            BinaryFormatter bformatter = new BinaryFormatter();
            db = (SettingsObject)bformatter.Deserialize(mem);
            mem.Close();
            return db;
        }

        public static string Duzelt(this string val, int len)
        {
            if (val.Length >= len)
            {
                return val.Substring(0, len);
            }
            else
            {
                return val.PadRight(len);
            }
        }
    }
}
